package com.lphj.datainterface.controller;

import com.lphj.datainterface.entity.TGoods;
import com.lphj.datainterface.handle.TGoodsForMCHandle;
import com.lphj.datainterface.handle.TGoodsHandle;
import com.lphj.datainterface.handle.TGoodsSheetHandle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;


@RestController
public class ReadGoodsController {

    @Autowired
    TGoodsHandle tGoodsHandle;

    @Autowired
    TGoodsSheetHandle tGoodsSheetHandle;

    @Autowired
    TGoodsForMCHandle tGoodsForMCHandle;

    /**
     * 更新商品信息
     * @param excel
     * @return
     */
    @RequestMapping("readGoods")
    String readExcelForGoods(@RequestParam("customerExcel") MultipartFile excel) {
        String info = "";
        boolean isExcel2003 = true;
        String fileName = excel.getOriginalFilename();
        //判断文件是否有效
        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            info = "文件无效";
            return info;
        }
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }
        InputStream is = null;
        Workbook wb = null;
        try {
            is = excel.getInputStream();
            if (isExcel2003) {
                wb = new HSSFWorkbook(is);
            } else {
                wb = new XSSFWorkbook(is);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取第一个商品表
        Sheet sheet = wb.getSheetAt(0);
        //写入分公司商品 分公司商品供应商
        info = "成功插入" + tGoodsHandle.addTGoods(sheet) + "条商品记录!";
        //获取sGoodsNO2不等于空 即观麦商品信息
        List<TGoods> tGoodsList = tGoodsHandle.getTGoodsByGoodsNO2();
        //商品资料维护变更 商品 公司 客户 三者之间的关系表
        info += "商品资料新增" + tGoodsSheetHandle.addTGoodsSheet(tGoodsList) + "条记录!";
        return info;
    }

    /**
     * 美菜系统同步商品信息
     * @param excel
     * @return
     */
    @RequestMapping("readGoods_mc")
    String readExcelForGoods_mc(@RequestParam("customerExcel") MultipartFile excel) {
        String info = "";
        boolean isExcel2003 = true;
        String fileName = excel.getOriginalFilename();
        //判断文件是否有效
        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            info = "文件无效";
            return info;
        }
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }
        InputStream is = null;
        Workbook wb = null;
        try {
            is = excel.getInputStream();
            if (isExcel2003) {
                wb = new HSSFWorkbook(is);
            } else {
                wb = new XSSFWorkbook(is);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取第一个商品表
        Sheet sheet = wb.getSheetAt(0);
        Map<String,Object> data = tGoodsForMCHandle.unifiedTGoods(sheet);
        return data.toString();
    }
}
